package com.hejjon.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hejjon.base.BaseController;
import com.hejjon.bean.FileStoreInfo;
import com.hejjon.bean.ResponseResult;
import com.hejjon.entity.FileModel;
import com.hejjon.service.IFileManageService;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Api(tags = "文件管理")
public class FileManageController extends BaseController {


    @Autowired
    private IFileManageService fileManageService;

    @PostMapping("/upload")
    public ResponseResult<?> upload(@RequestParam("file") MultipartFile file) {
        FileStoreInfo fileStoreInfo = fileManageService.uploadFile(file);
        return success(fileStoreInfo);
    }

    @GetMapping("/list")
    public ResponseResult<?> list(@RequestParam String fileName) {
        LambdaQueryWrapper<FileModel> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(fileName), FileModel::getFileName, fileName);
        List<FileModel> list = fileManageService.list(lqw);
        return success(list);
    }

    @GetMapping("/download")
    public void download(HttpServletResponse response, @RequestParam String fileId) {
        fileManageService.download(response, fileId);
    }


}
